package com.tarena.vehicle.management.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.vehicle.common.ex.ServiceException;
import com.tarena.vehicle.common.pojo.vo.PageData;
import com.tarena.vehicle.common.util.PageInfoToPageDataConverter;
import com.tarena.vehicle.common.web.ServiceCode;
import com.tarena.vehicle.management.converter.VehicleConverter;
import com.tarena.vehicle.management.mapper.VehicleMapper;
import com.tarena.vehicle.management.pojo.entity.Vehicle;
import com.tarena.vehicle.management.pojo.param.VehicleAddNewParam;
import com.tarena.vehicle.management.pojo.param.VehicleUpdateParam;
import com.tarena.vehicle.management.pojo.query.VehicleQuery;
import com.tarena.vehicle.management.pojo.vo.VehicleListItemVO;
import com.tarena.vehicle.management.service.IVehicleService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VehicleServiceImpl implements IVehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private VehicleConverter vehicleConverter;

    private static final Logger logger= LoggerFactory.getLogger(VehicleServiceImpl.class);

    @Override public void addNew(VehicleAddNewParam param) {
        String license = param.getLicense();

        int count=vehicleMapper.getCountByLicense(license);

        if (count>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"当前车牌已经注册");
        }
        Vehicle vehicle = vehicleConverter.param2po(param);
        logger.info("车辆信息如下{}",vehicle);
        int rows=vehicleMapper.insert(vehicle);
        if (rows!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "添加车辆信息失败，服务器忙，请稍后再尝试！");
        }

    }

    @Override public PageData<VehicleListItemVO> getAll(VehicleQuery query, Integer num, Integer size) {
        Vehicle vehicle = vehicleConverter.query2po(query);
        if (num!=null&&size!=null)
            PageHelper.startPage(num,size);
        List<VehicleListItemVO> list = vehicleMapper.list(vehicle);
        PageInfo<VehicleListItemVO> pageInfo = new PageInfo<>(list);
        PageData<VehicleListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override public void deleteById(Integer id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        List<VehicleListItemVO> list = vehicleMapper.list(vehicle);
        if (list.isEmpty()){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该车辆信息不存在");
        }
        int row=vehicleMapper.deleteById(id);
        if (row!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "删除车辆信息失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void update(VehicleUpdateParam param) {
        Vehicle vehicle = vehicleConverter.param2po(param);
        int row=vehicleMapper.update(vehicle);
        if (row<1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆信息失败，服务器忙，请稍后再尝试！");
        }
    }
}
